package com.ruoyi.project.mahjong.controller;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.TIntegral;
import com.ruoyi.project.mahjong.service.ITIntegralService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 积分赠送Controller
 *
 * @author ruoyi
 * @date 2023-09-28
 */
@RestController
@RequestMapping("/mahjong/integral")
public class TIntegralController extends BaseController
{
    @Autowired
    private ITIntegralService tIntegralService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询积分赠送列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:integral:list')")
    @GetMapping("/list")
    public TableDataInfo list(TIntegral tIntegral)
    {
        startPage();
        List<TIntegral> list = tIntegralService.selectTIntegralList(tIntegral);
        return getDataTable(list);
    }
    @GetMapping("/getIntegral")
    public AjaxResult getIntegral()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
       List<TIntegral> list = tIntegralService.getIntegral(shopId);
        return AjaxResult.success(list);
    }
    /**
     * 导出积分赠送列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:integral:export')")
    @Log(title = "积分赠送", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TIntegral tIntegral)
    {
        List<TIntegral> list = tIntegralService.selectTIntegralList(tIntegral);
        ExcelUtil<TIntegral> util = new ExcelUtil<TIntegral>(TIntegral.class);
        return util.exportExcel(list, "integral");
    }

    /**
     * 获取积分赠送详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:integral:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tIntegralService.selectTIntegralById(id));
    }

    /**
     * 新增积分赠送
     */
    @PreAuthorize("@ss.hasPermi('mahjong:integral:add')")
    @Log(title = "积分赠送", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TIntegral tIntegral)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tIntegral.setShopId(shopId);
        return toAjax(tIntegralService.insertTIntegral(tIntegral));
    }

    /**
     * 修改积分赠送
     */
    @PreAuthorize("@ss.hasPermi('mahjong:integral:edit')")
    @Log(title = "积分赠送", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TIntegral tIntegral)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tIntegral.setShopId(shopId);
        return toAjax(tIntegralService.updateTIntegral(tIntegral));
    }

    /**
     * 删除积分赠送
     */
    @PreAuthorize("@ss.hasPermi('mahjong:integral:remove')")
    @Log(title = "积分赠送", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tIntegralService.deleteTIntegralByIds(ids));
    }
}
